package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryThirteenMonthDao;
import com.boss.salary.entity.SalaryMonthEnd;
import com.boss.sys.utils.SysUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Repository
public class SalaryThirteenMonthDaoImpl extends BaseDaoImpl implements ISalaryThirteenMonthDao {

    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final int IS_THIRTEEN = 1; //是十三月工资
    private static final int NO_THIRTEEN = 0; //不是十三月工资


    @Override
    public void batchInsertSalaryThirteenMonthDao(List<SalaryMonthEnd> list) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        LocalDateTime localDateTime = LocalDateTime.now();
        List<Object> params = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        StringBuilder columnBuilder = new StringBuilder();
        StringBuilder paramsBuilder = new StringBuilder();

        String columnNames = list.get(0).getColumnNames();
        if (!StringUtils.isEmpty(columnNames)) {
            String[] columnNameArr = columnNames.split(",");
            for (String columnName : columnNameArr) {
                columnBuilder.append(" , ").append(columnName);
            }
            for (SalaryMonthEnd salaryMonthEnd : list) {
                paramsBuilder.append("(?,?,?,?,?,?");
                params.add(SysUtils.generateGuid());
                params.add(localDateTime.format(dtf));
                params.add(localDateTime.format(dtf));
                params.add(salaryMonthEnd.getAgencyId());
                params.add(salaryMonthEnd.getPersonId());
                params.add(IS_THIRTEEN);
                for (String columnName : columnNameArr) {
                    paramsBuilder.append(",?");
                    Class<?> clazz = salaryMonthEnd.getClass();
                    Method getMethod = clazz.getDeclaredMethod("get" +
                            columnName.substring(0, 1).toUpperCase() + columnName.substring(1));
                    params.add(getMethod.invoke(salaryMonthEnd));
                }
                paramsBuilder.append(" ) ");
                if (list.indexOf(salaryMonthEnd) != list.size() - 1) {
                    paramsBuilder.append(" , ");
                }
            }
        }
        sb.append(" insert into ")
                .append(TableName.SALARY_THIRTEEN_MONTH)
                .append(" (id, create_time, update_time, agency_id, person_id, is_thirteen ")
                .append(columnBuilder)
                .append(" ) ").append(" values")
                .append(paramsBuilder);
        super.getjdbcTemplate().update(sb.toString(), params.toArray());
    }


}

